<template>
  <div class="login">
    <div class="show-container">
      <div class="left-pic" v-if="!isMobile">
        <div class="login-box">
          <div class="logo"></div>
          <span style="font-size: 28px;font-weight: bold;margin-left: 10px;">{{ site_title }}</span>
        </div>
        <div style="font-size: 1.2rem;">{{ site_subtitle }}</div>
        <div style="font-size: 14px;line-height: 1.5em;margin-left: 10px;">
          <div v-for="(item,index) in site_note" :index="index">{{ item }}</div>
        </div>
      </div>
      <div class="login-container">
        <h2 style="margin-bottom: 20px;">
          <span v-if="isMobile">Simplest Admin</span>
          <span v-else><h4>登录</h4></span>
        </h2>
        <el-form ref="login" :model="login" size="medium" :rules="loginRules" label-position="left">
          <el-form-item prop="username">
            <el-input
                v-model="login.username"
                type="text"
                placeholder="用户名"
                prefix-icon="el-icon-user"
                autocomplete="off"
                clearable/>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
                v-model="login.password"
                type="text"
                placeholder="密码"
                prefix-icon="el-icon-lock"
                @keyup.enter.native="handleLogin"
                autocomplete="off"
                clearable
                show-password/>
          </el-form-item>
          <el-form-item prop="verify" v-if="verify_status">
            <el-input
                class="verify-input"
                v-model="login.verify"
                type="text"
                placeholder="验证码"
                prefix-icon="el-icon-picture"
                @keyup.enter.native="handleLogin"
                autocomplete="off"
                clearable/>
            <el-image
                class="verify-img"
                :src="verifySrc"
                alt="验证码"
                title="点击刷新验证码"
                @click="refeshVerifySrc"/>
          </el-form-item>
        </el-form>
        <el-checkbox v-if="rememberMe" v-model="login.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>
        <el-button
            :loading="loading"
            size="small"
            type="primary"
            style="width:100%;"
            @click.native.prevent="handleLogin">
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
      </div>
    </div>
    <div class="copyright">{{copyright}}</div>
  </div>
</template>

<script>
import {captche} from '@/api/admin/base'
import {encrypt, decrypt} from '@/utils/jsencrypt'
import MyUtils from "@/utils/common";

export default {
  data() {
    return {
      loading: false,
      login: {
        username: 'super',
        password: 'admin123',
      },
      loginRules: {
        username: [{required: true, message: '请输入用户名', trigger: 'blur'}],
        password: [{required: true, message: '请输入密码', trigger: 'blur'}],
        verify: [{required: true, message: '请输入验证码', trigger: 'blur'}]
      },
      verifySrc: '',
      verify_status: true,
      rememberMe: true,
      success_url: '',
      site_title: '',
      copyright: '',
      site_subtitle: '',
      site_note: [],
      isMobile: false,
    }
  },
  created() {
    this.captche()
    this.getCookie()
    this.isMobile =  MyUtils.isMobile();
  },
  methods: {
    captche() {
      captche().then(res => {
        if (res.status == 200) {
          this.verifySrc = res.data.img
          this.login.key = res.data.key
          this.rememberMe = res.rememberMe
          this.success_url = res.success_url
          this.site_title = res.site_title
          this.site_subtitle = res.site_subtitle
          this.site_note = res.site_note.split('\n')
          this.copyright = res.copyright
          this.verify_status = res.verify_status
        } else {
          this.$message.error('验证码获取失败!')
        }
      })
    },
    refeshVerifySrc() {
      this.captche()
    },
    getCookie() {
      const username = this.$cookies.get("username")
      const password = this.$cookies.get("password")
      const rememberMe = this.$cookies.get('rememberMe')

      this.login = {
        username: username === null ? this.login.username : username,
        password: password === null ? this.login.password : decrypt(password),
        rememberMe: rememberMe === null ? false : Boolean(rememberMe)
      }
    },
    handleLogin() {
      this.$refs['login'].validate(valid => {
        if (valid) {
          // console.log('表单验证成功')
          this.loading = true
          if (this.login.rememberMe) {
            this.$cookies.set("username", this.login.username, {expires: 24 * 3600 * 15})
            this.$cookies.set("password", encrypt(this.login.password), {expires: 24 * 3600 * 15})
            this.$cookies.set('rememberMe', this.login.rememberMe, {expires: 24 * 3600 * 15})
          } else {
            this.$cookies.remove("username")
            this.$cookies.remove("password")
            this.$cookies.remove('rememberMe');
          }

          this.$store.dispatch('login', this.login).then(res => {
            this.$router.push(this.success_url)
            this.loading = false
          }).catch(err => {
            this.loading = false
            this.captche()
          })
        }
      })
    }
  },
}
</script>

<style lang="scss">
.login {
  background: url(../../../assets/images/bg008.svg) no-repeat;
  background-size: cover;
  min-height: 100vh;
  .show-container {
    width: 100%;
    min-height: 90vh;
    display: flex;
    justify-content: center;
    align-items: center;
    .left-pic{
      box-sizing: border-box;
      width: 380px;
      height: 380px;
      background: linear-gradient(0deg,#3a485a 0%,#607089 100%);
      padding: 20px 30px 80px 30px;
      box-shadow: 0 0 2px #ffffff4d;
      display: flex;
      justify-content: space-around;
      align-items: flex-start;
      flex-direction: column;
      color: #fff;
      .login-box {
        width: 100%;
        display: flex;
        align-items: center;
        justify-content: flex-start;
        font-size: 1rem;
        .logo {
          font-size: 1rem;
          padding: 5px;
          display: inline-block;
          background: url(../../../assets/images/logo.png) no-repeat;
          width: 20px;
          height: 20px;
          margin-left: 10px;
          background-size: cover;
        }
      }
    }
    .login-container {
      box-sizing: border-box;
      width: 380px;
      height: 380px;
      background: rgb(255,255,255);
      padding: 40px 60px;
      box-shadow: 0 0 10px #0000004d;
      .verify-input {
        vertical-align:center;
        width:60%;
        height:35px;
      }
      .verify-img {
        width: 40%;
        vertical-align:center;
        display: inline-block;
        height:35px;
        float:right
      }
    }
  }
  .copyright {
    display: flex;
    justify-content: center;
    padding: 20px;
    font-size: 0.6em;
    word-wrap: break-word;
    text-align: center;
    line-height: 1.2em;
  }
}
</style>
